package com.norma.netty.study.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @author zhangchaofeng
 * @date 2020/10/4
 * @description 心跳处理器
 */
public class NettyHeartbeatServerHandler extends ChannelInboundHandlerAdapter {

    //evt 事件
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            //将evt向下转型
            IdleStateEvent event = (IdleStateEvent) evt;
            String status = "";
            switch (event.state()) {
                case READER_IDLE:
                    status = "读空闲";
                    break;
                case WRITER_IDLE:
                    status = "写空闲";
                    break;
                case ALL_IDLE:
                    status = "读写空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress() + status);
            ctx.channel().closeFuture();
        }

    }

}
